<!DOCTYPE HTML>
<html lang="en">
	<head>
		<title>three.js - geometry - sphere</title>
		<meta charset="utf-8">
		<style type="text/css">
			body {
				font-family: Monospace;
				background-color: #f0f0f0;
				margin: 0px;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

		<!-- <script type="text/javascript" src="../build/Three.js"></script> -->

		<script type="text/javascript" src="../src/Three.js"></script>
		<script type="text/javascript" src="../src/core/Color.js"></script>
		<script type="text/javascript" src="../src/core/Vector2.js"></script>
		<script type="text/javascript" src="../src/core/Vector3.js"></script>
		<script type="text/javascript" src="../src/core/Vector4.js"></script>
		<script type="text/javascript" src="../src/core/Ray.js"></script>
		<script type="text/javascript" src="../src/core/Rectangle.js"></script>
		<script type="text/javascript" src="../src/core/Matrix3.js"></script>
		<script type="text/javascript" src="../src/core/Matrix4.js"></script>
		<script type="text/javascript" src="../src/core/Vertex.js"></script>
		<script type="text/javascript" src="../src/core/Face3.js"></script>
		<script type="text/javascript" src="../src/core/Face4.js"></script>
		<script type="text/javascript" src="../src/core/UV.js"></script>
		<script type="text/javascript" src="../src/core/Geometry.js"></script>
		<script type="text/javascript" src="../src/cameras/Camera.js"></script>
		<script type="text/javascript" src="../src/lights/Light.js"></script>
		<script type="text/javascript" src="../src/lights/AmbientLight.js"></script>
		<script type="text/javascript" src="../src/lights/DirectionalLight.js"></script>
		<script type="text/javascript" src="../src/lights/PointLight.js"></script>
		<script type="text/javascript" src="../src/objects/Object3D.js"></script>
		<script type="text/javascript" src="../src/objects/Particle.js"></script>
		<script type="text/javascript" src="../src/objects/Line.js"></script>
		<script type="text/javascript" src="../src/objects/Mesh.js"></script>
		<script type="text/javascript" src="../src/materials/Material.js"></script>
		<script type="text/javascript" src="../src/materials/LineBasicMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshBasicMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshLambertMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshPhongMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshDepthMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshNormalMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshFaceMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/MeshShaderMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/ParticleBasicMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/ParticleCircleMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/ParticleDOMMaterial.js"></script>
		<script type="text/javascript" src="../src/materials/Texture.js"></script>
		<script type="text/javascript" src="../src/materials/mappings/CubeReflectionMapping.js"></script>
		<script type="text/javascript" src="../src/materials/mappings/CubeRefractionMapping.js"></script>
		<script type="text/javascript" src="../src/materials/mappings/LatitudeReflectionMapping.js"></script>
		<script type="text/javascript" src="../src/materials/mappings/LatitudeRefractionMapping.js"></script>
		<script type="text/javascript" src="../src/materials/mappings/SphericalReflectionMapping.js"></script>
		<script type="text/javascript" src="../src/materials/mappings/SphericalRefractionMapping.js"></script>
		<script type="text/javascript" src="../src/materials/mappings/UVMapping.js"></script>
		<script type="text/javascript" src="../src/scenes/Scene.js"></script>
		<script type="text/javascript" src="../src/scenes/Fog.js"></script>
		<script type="text/javascript" src="../src/renderers/Projector.js"></script>
		<script type="text/javascript" src="../src/renderers/DOMRenderer.js"></script>
		<script type="text/javascript" src="../src/renderers/CanvasRenderer.js"></script>
		<script type="text/javascript" src="../src/renderers/SVGRenderer.js"></script>
		<script type="text/javascript" src="../src/renderers/WebGLRenderer.js"></script>
		<script type="text/javascript" src="../src/renderers/renderables/RenderableObject.js"></script>
		<script type="text/javascript" src="../src/renderers/renderables/RenderableFace3.js"></script>
		<script type="text/javascript" src="../src/renderers/renderables/RenderableParticle.js"></script>
		<script type="text/javascript" src="../src/renderers/renderables/RenderableLine.js"></script>

		<script type="text/javascript" src="../src/extras/primitives/Sphere.js"></script>
		<script type="text/javascript" src="../src/extras/ImageUtils.js"></script>

		<script type="text/javascript" src="js/Stats.js"></script>

		<script type="text/javascript">

			var container, stats;

			var camera, scene, renderer, objects;
			var pointLight;

			var sphere;

			var targetRotation = 0;
			var targetRotationOnMouseDown = 0;

			var mouseX = 0;
			var mouseXOnMouseDown = 0;

			var windowHalfX = window.innerWidth / 2;
			var windowHalfY = window.innerHeight / 2;

			var moveForward = false, moveBackwards = false,
			moveUp = false, moveDown = false,
			moveLeft = false, moveRight = false,

			yawLeft = false, yawRight = false,
			pitchUp = false, pitchDown = false,
			rollLeft = false, rollRight = false;

			var debugContext;

			init();
			setInterval( loop, 1000 / 60 );

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
				camera.position.y = 150;
				camera.position.z = 400;
				camera.target.position.y = 150;

				scene = new THREE.Scene();

				// Grid

				var geometry = new THREE.Geometry();
				geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, 0, 0 ) ) );
				geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 500, 0, 0 ) ) );

				var material = new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.5 } );

				for ( var i = 0; i <= 10; i ++ ) {

					var line = new THREE.Line( geometry, material );
					line.position.z = ( i * 100 ) - 500;
					scene.addObject( line );

					var line = new THREE.Line( geometry, material );
					line.position.x = ( i * 100 ) - 500;
					line.rotation.y = 90 * Math.PI / 180;
					scene.addObject( line );

				}

				// Spheres

				objects = [];

				geometry = new Sphere( 100, 16, 8 );
				material = new THREE.MeshBasicMaterial( { env_map: ImageUtils.loadTexture( 'textures/envmap.png', new THREE.SphericalReflectionMapping() ) } );

				for ( var i = 0; i < 10; i ++ ) {

					sphere = new THREE.Mesh( geometry, material );
					sphere.overdraw = true;

					sphere.position.x = Math.random() * 1000 - 500;
					sphere.position.y = Math.random() * 1000 - 500;
					sphere.position.z = Math.random() * 1000 - 500;

					sphere.rotation.x = Math.random() * 200 - 100;
					sphere.rotation.y = Math.random() * 200 - 100;
					sphere.rotation.z = Math.random() * 200 - 100;

					sphere.scale.x = sphere.scale.y = sphere.scale.z = Math.random() + 0.5;

					objects.push( sphere );

					scene.addObject( sphere );

				}

				// Lights

				var ambientLight = new THREE.AmbientLight( Math.random() * 0x202020 );
				scene.addLight( ambientLight );

				var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
				directionalLight.position.x = Math.random() - 0.5;
				directionalLight.position.y = Math.random() - 0.5;
				directionalLight.position.z = Math.random() - 0.5;
				directionalLight.position.normalize();
				scene.addLight( directionalLight );

				pointLight = new THREE.PointLight( 0xff0000, 1 );
				scene.addLight( pointLight );

				renderer = new THREE.CanvasRenderer();
				renderer.setSize( window.innerWidth, window.innerHeight );

				container.appendChild( renderer.domElement );

				var debugCanvas = document.createElement( 'canvas' );
				debugCanvas.width = 512;
				debugCanvas.height = 512;
				debugCanvas.style.position = 'absolute';
				debugCanvas.style.top = '0px';
				debugCanvas.style.left = '0px';

				container.appendChild( debugCanvas );

				debugContext = debugCanvas.getContext( '2d' );
				debugContext.setTransform( 1, 0, 0, 1, 256, 256 );
				debugContext.strokeStyle = '#000000';

				stats = new Stats();
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.top = '0px';
				container.appendChild(stats.domElement);

				document.addEventListener( 'keydown', onDocumentKeyDown, false );
				document.addEventListener( 'keyup', onDocumentKeyUp, false );
			}

			function onDocumentKeyDown( event ) {

				switch( event.keyCode ) {

					case 38: moveForward = true; break; // up
					case 40: moveBackwards = true; break; // down
					case 37: moveLeft = true; break; // left
					case 39: moveRight = true; break; // right
					case 65: yawLeft = true; break; // a
					case 68: yawRight = true; break; // d
					case 87: moveUp/*pitchUp*/ = true; break; // w
					case 83: moveDown/*pitchDown*/ = true; break; // s
					case 90: rollLeft = true; break; // z
					case 67: rollRight = true; break; // c

				}

			}

			function onDocumentKeyUp( event ) {

				switch( event.keyCode ) {

					case 38: moveForward = false; break; // up
					case 40: moveBackwards = false; break; // down
					case 37: moveLeft = false; break; // left
					case 39: moveRight = false; break; // right
					case 65: yawLeft = false; break; // a
					case 68: yawRight = false; break; // d
					case 87: moveUp/*pitchUp*/ = false; break; // w
					case 83: moveDown/*pitchDown*/ = false; break; // s
					case 90: rollLeft = false; break; // z
					case 67: rollRight = false; break; // c

				}

			}

			//

			function loop() {

				if ( moveForward ) camera.position.z -= 5;
				if ( moveBackwards ) camera.position.z += 5;

				if ( moveUp ) camera.position.y += 5;
				if ( moveDown ) camera.position.y -= 5;

				if ( moveLeft ) camera.position.x -= 5;
				if ( moveRight ) camera.position.x += 5;

				if ( pitchUp ) camera.rotation.x += 0.01;
				if ( pitchDown ) camera.rotation.x -= 0.01;

				if ( yawLeft ) camera.target.position.x -= 5;
				if ( yawRight ) camera.target.position.x += 5;

				if ( rollLeft ) camera.rotation.z += 0.01;
				if ( rollRight ) camera.rotation.z -= 0.01;

				debugContext.clearRect( -256, -256, 512, 512 );

				debugContext.beginPath();

				// center
				debugContext.moveTo( -10, 0 );
				debugContext.lineTo( 10, 0 );
				debugContext.moveTo( 0, -10 );
				debugContext.lineTo( 0, 10 );

				// camera

				debugContext.moveTo( camera.position.x * 0.1, camera.position.z * 0.1 );
				debugContext.lineTo( camera.target.position.x * 0.1, camera.target.position.z * 0.1 );
				debugContext.rect( camera.position.x * 0.1 - 5, camera.position.z * 0.1 - 5, 10, 10 );
				debugContext.rect( camera.target.position.x * 0.1 - 5, camera.target.position.z * 0.1 - 5, 10, 10 );
				debugContext.rect( - 50, - 50, 100, 100 );

				for ( var i = 0, l = objects.length; i < l; i++ ) {

					var object = objects[ i ];

					object.rotation.x += 0.01;
					object.rotation.y += 0.005;
					object.position.y = Math.sin( object.rotation.x ) * 200;

					debugContext.rect( object.position.x * 0.1 - 5, object.position.z * 0.1 - 5, 10, 10 );

				}


				debugContext.closePath();
				debugContext.stroke();

				renderer.render(scene, camera);

				stats.update();
			}

		</script>

	</body>
</html>
